﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using TMDT_Nhom1A.DTO;


namespace TMDT_Nhom1A.DAO
{
    public class TranportWebserviceDAO
    {
        private static QL_DoanhNghiepDataContext qldoanhnghiep = new QL_DoanhNghiepDataContext();

        public static List<TRANSPORT_WEBSERVICE> getAllTransportWebservice()
        {
            List<TRANSPORT_WEBSERVICE> transportWebservices = new List<TRANSPORT_WEBSERVICE>();
            try
            {
                var result = from trans in qldoanhnghiep.TRANSPORT_WEBSERVICEs
                             select trans;
                foreach (var resultItem in result)
                {
                    transportWebservices.Add(resultItem);
                }
                return transportWebservices;
            }
            catch (Exception ex)
            {
                return null;
            }
        }


        internal static bool insertTransportWebservice(TRANSPORT_WEBSERVICE transportWS)
        {
            
            try
            {
                bool result = false;
                qldoanhnghiep.TRANSPORT_WEBSERVICEs.InsertOnSubmit(transportWS);
                qldoanhnghiep.SubmitChanges();

                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
       
        }

        internal static bool updateTransportWebservice(TRANSPORT_WEBSERVICE transportWS)
        {
            try
            {
                TRANSPORT_WEBSERVICE oldTransport = (from trans in qldoanhnghiep.TRANSPORT_WEBSERVICEs
                                                     where trans.TRANSPORT_WS_ID == transportWS.TRANSPORT_WS_ID
                                                     select trans).Single();
                oldTransport.TransportUrl = transportWS.TransportUrl;
                oldTransport.TransportCompanyName = transportWS.TransportCompanyName;
                oldTransport.Description = transportWS.Description;

                qldoanhnghiep.SubmitChanges();

                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public static bool deleteTransportWebservice(int transportWsId)
        {
            try
            {
                TRANSPORT_WEBSERVICE oldTransport = (from trans in qldoanhnghiep.TRANSPORT_WEBSERVICEs
                                                     where trans.TRANSPORT_WS_ID == transportWsId
                                                     select trans).Single();
                var accountTransportWsResult = from acc in qldoanhnghiep.AccountTransportWebservices
                                               where acc.TransportWS_ID == transportWsId
                                               select acc;
                if(accountTransportWsResult.Count() >=1)
                    qldoanhnghiep.AccountTransportWebservices.DeleteAllOnSubmit(accountTransportWsResult);

                qldoanhnghiep.TRANSPORT_WEBSERVICEs.DeleteOnSubmit(oldTransport);
                qldoanhnghiep.SubmitChanges();

                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}
